//
//  NumberKeyboard.m
//  ZITHER
//
//  Created by weixin on 2018/8/2.
//  Copyright © 2018年 weixn. All rights reserved.
//

#import "NumberKeyboard.h"
#import "MeRegisterVC.h"
#import<AudioToolbox/AudioToolbox.h>

@implementation NumberKeyboard

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self Keyboard];
    }
    return self;
}


-(void)addBtn{
    
    CGFloat viewH = 240;
    
    for (int i =0; i<12; i++) {
        
        QMUIButton *btn = [[QMUIButton alloc]init];
        
        btn.tag = 101+i;
        
        CGFloat h = viewH/4.0;
        
        CGFloat w = SCREEN_WIDTH/3.0;
        
        btn.frame = CGRectMake(i%3*w,i/3*h,w,h);
        
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [self addSubview:btn];
        
        [btn setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
        
        
        //处理按钮点击事件
        [btn addTarget:self action:@selector(TouchDown:)forControlEvents: UIControlEventTouchDragInside];
        //处理按钮松开状态
        [btn addTarget:self action:@selector(TouchUp:)forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
        
        @weakify(self);
        [[btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (self.BtnBlock) {
                self.BtnBlock(x.tag - 100);
            }
        }];



        if (i==9) {
            [btn setTitle:@"" forState:UIControlStateNormal];
        }else if (i==10){
            [btn setTitle:@"0" forState:UIControlStateNormal];
        }else if (i==11){
            [btn setTitle:@"" forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:@"log_btn_del"] forState:UIControlStateNormal];
            //            [btn setTitle:@"删除" forState:UIControlStateNormal];
            
        }
        
    }
}

-(void)addLab{
    
    CGFloat viewH = 240;
    
    for (int i =0; i<12; i++) {
    
        
        
        CGFloat h = viewH/4.0;
        
        CGFloat w = SCREEN_WIDTH/3.0;
        BaseLab *btn = [[BaseLab alloc]initWithFrame:CGRectMake(i%3*w,i/3*h,w,h)];
        btn.tag = 101+i;
        btn.textAlignment = UITextAlignmentCenter;
        btn.textColor = [UIColor whiteColor];
        
        btn.text = [NSString stringWithFormat:@"%d",i+1];
        
        [self addSubview:btn];
        
        
        /** 添加点击事件 */
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        @weakify(self);
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            @strongify(self);
            if (self.BtnBlock) {
                self.BtnBlock(btn.tag - 100);
            }
        }];
        btn.userInteractionEnabled = YES;
        [btn addGestureRecognizer:tap];
        
        
        if (i==9) {
            btn.text = @"";
        }else if (i==10){
            btn.text = @"0";
        }else if (i==11){
            btn.text = @"";

//            [btn setImage:[UIImage imageNamed:@"log_btn_del"] forState:UIControlStateNormal];
            //            [btn setTitle:@"删除" forState:UIControlStateNormal];
        }
     
        if (i==11) {
            CGFloat h = viewH/4.0;
            CGFloat w = SCREEN_WIDTH/3.0;
            BaseImageV *img = [[BaseImageV alloc]initializeFrame:CGRectMake(i%3*w,i/3*h,w/2,h/2) contentMode:1];
            img.tag = 101+i;
            img.image = [UIImage imageNamed:@"log_btn_del"];
            img.backgroundColor = [UIColor clearColor];
            [self addSubview:img];
            img.center = btn.center;
            
            /** 添加点击事件 */
//            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
//            @weakify(self);
//            [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
//                @strongify(self);
//                if (self.BtnBlock) {
//                    self.BtnBlock(img.tag - 100);
//                }
//            }];
//            img.userInteractionEnabled = YES;
//            [img addGestureRecognizer:tap];
            
            return;
        }
        
    }
}
-(void)Keyboard{
    
    CGFloat viewH = 240;
    
//    [self addBtn];
    [self addLab];
//    for (int i =0; i<12; i++) {
//
//        QMUIButton *btn = [[QMUIButton alloc]init];
//
//        btn.tag = 101+i;
//
//        CGFloat h = viewH/4.0;
//
//        CGFloat w = SCREEN_WIDTH/3.0;
//
//        btn.frame = CGRectMake(i%3*w,i/3*h,w,h);
//
//        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//
//        [self addSubview:btn];
//
//        [btn setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
//
//
//        //处理按钮点击事件
//        [btn addTarget:self action:@selector(TouchDown:)forControlEvents: UIControlEventTouchDragInside];
//        //处理按钮松开状态
//        [btn addTarget:self action:@selector(TouchUp:)forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
//
//        @weakify(self);
////        [[btn rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
////            @strongify(self);
////            if (weakSelf.BtnBlock) {
////                weakSelf.BtnBlock(x.tag - 100);
////            }
////
////        }];
//
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
//        self.userInteractionEnabled = YES;
//        [[tap rac_gestureSignal]subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
//            @strongify(self);
//            if (self.BtnBlock) {
////                NSLog(@"btn.tag==================%d",btn.tag);
//                self.BtnBlock(btn.tag - 100);
//                if(btn.tag!=110){
//                    //这里可以处理其他点击事件
//                    btn.backgroundColor = [UIColor clearColor];
//                    NSLog(@"按钮松开了");
//                }
//            }
//        }];
//        [btn addGestureRecognizer:tap];

        
//        if (i==9) {
//            [btn setTitle:@"" forState:UIControlStateNormal];
//        }else if (i==10){
//            [btn setTitle:@"0" forState:UIControlStateNormal];
//        }else if (i==11){
//            [btn setTitle:@"" forState:UIControlStateNormal];
//            [btn setImage:[UIImage imageNamed:@"log_btn_del"] forState:UIControlStateNormal];
//            //            [btn setTitle:@"删除" forState:UIControlStateNormal];
//
//        }
    
//    }
    
    for (int i =0; i<5; i++) {
        UIView *Line = [[UIView alloc]init];
        Line.alpha = 0.5;
        Line.backgroundColor = [UIColor whiteColor];
        
        
        CGFloat x = 0;
        CGFloat y = 0;
        CGFloat w = SCREEN_WIDTH;
        CGFloat h = viewH;
        
        if (i<2) {
            x = SCREEN_WIDTH/3.0*(i+1);
            w = 1;
        }else{
            y += viewH/4.0*(i-1);
            h = 1;
        }
        Line.frame = CGRectMake(x, y, w, h);
        [self addSubview:Line];
    }
    
}



- (void)TouchDown:(UIButton*)btn
{
    if(btn.tag!=110){
        //这里可以处理其他点击事件
        btn.backgroundColor = [UIColor whiteColor];
        btn.alpha = 0.3;
    }
  
}

- (void)TouchUp:(UIButton*)btn
{
    if(btn.tag!=110){
        //这里可以处理其他点击事件
        btn.backgroundColor = [UIColor clearColor];
        NSLog(@"按钮松开了");
    }
}

@end
